﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SEBanking
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"].ToString() == String.Empty)
                Response.Redirect("~/Index.aspx");

            DataClassesSEBankingDataContext context = new DataClassesSEBankingDataContext();
            var query = from a in context.SEBanking_Accounts
                        from c in context.SEBanking_Cards
                        where a.Acc_ID == Session["UserID"].ToString() && a.Acc_ID == c.Acc_ID
                        select new { CardID = c.Card_ID, SecurityCode = c.Card_SecurityCode, Balance = c.Card_Banlance };
            foreach (var q in query)
            {
                lblCardID.Text = q.CardID;
                lblSecurityCode.Text = q.SecurityCode;
                lblBalance.Text = (q.Balance).ToString();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            decimal money = 0;
            try
            {
                money = decimal.Parse(txtMoney.Text);
            }
            catch (System.Exception ex)
            {
                lblResult.Text = "The number of money is not match";
            }
            ServiceReferenceSEBanking.Service1Client sw = new ServiceReferenceSEBanking.Service1Client();
            int result = sw.SendIn(lblCardID.Text, money);
            switch (result)
            {
                case -1:
                    lblResult.Text = "ID of card do not exist";
            	    break;
                case 0:
                    lblResult.Text = "The number of money is not match";
                    break;
                case 1:
                    lblResult.Text = "Exchange succeeded";
                    DataClassesSEBankingDataContext context = new DataClassesSEBankingDataContext();
                    var query = context.SEBanking_Cards.Single(c => c.Card_ID == lblCardID.Text);
                    lblBalance.Text = query.Card_Banlance.ToString();
                    break; 
            }
        }
    }
}
